﻿using JshRCSWebApi.Infrastructure;
using JshRCSWebApi.Infrastructure.Domain.DTO.SysService;
using JshRCSWebApi.Infrastructure.Domain.VO.SysService;
using JshRCSWebApi.Service.SysService;
using JshRCSWebApi.SqlSugarDB.Model.jshrcs_initial;
using JshRCSWebApi.Util.Json;
using Npgsql.TypeHandlers;
using SqlSugar.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JshRCSWebApi.ServiceImpl.SysService
{
    public class DictionaryServiceImpl : DictionaryService
    {
        private readonly Repository<dynamic> _repository;
        public DictionaryServiceImpl(Repository<dynamic> repository)
        {
            this._repository = repository;
        }

        /// <summary>
        /// 新增字典
        /// </summary>
        /// <param name="addDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult> AddDictionary(DictionaryAddDTO addDTO,int UID)
        {

          int count = _repository.Add<servicedictionaries>(new servicedictionaries()
                        {
                            sdPassed = addDTO.sdPassed,
                            sdKey = addDTO.sdKey,
                            sdValue = addDTO.sdValue,
                            CreateBy = UID,
                            CreateTime = DateTime.Now
                        });
            return await Task.Run(async () =>
            {
                return count > 0 ? ApiResult.Success("新增成功") : ApiResult.Error("新增失败");
            });
        }

        /// <summary>
        /// 删除字典
        /// </summary>
        /// <param name="iD"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult> DeleteDictionary(long ID)
        {
            int count = _repository.Delete<servicedictionaries>(d => d.ID == ID );
            return await Task.Run(async () =>
            {
                return count > 0 ? ApiResult.Success("删除成功") : ApiResult.Error("删除失败");
            });
        }

        /// <summary>
        /// 根据key获取字典
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult> GetDictionaryByKey(string key)
        {
          var dicInfo =  _repository.Select<servicedictionaries>()
                                    .Where(d => d.sdKey == key)
                                    .First();
          var valueList = JsonHelper.ToObj<List<sdValueVO>>(dicInfo.sdValue);
          return await Task.Run(async () =>  ApiResult.Success("获取成功",valueList));
        }

        /// <summary>
        /// 获取字典集合
        /// </summary>
        /// <param name="searchDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult> GetDictionaryList(DictionarySearchDTO searchDTO)
        {
            int total = 0;
            var dicList =  _repository.Context.Queryable<servicedictionaries>()
                                       .WhereIF(!string.IsNullOrEmpty(searchDTO.sdKey), d => d.sdKey.Contains(searchDTO.sdKey))
                                       .WhereIF(searchDTO.stratTime != default(DateTime) && searchDTO.endTime != default(DateTime),d => d.CreateTime >= searchDTO.stratTime && d.CreateTime <= searchDTO.endTime)
                                       .ToPageList(searchDTO.pageNo, searchDTO.pageSize, ref total)
                                       .Select(d => new {ID = d.ID,sdKey = d.sdKey,sdValue = d.sdValue,sdPassed = d.sdPassed,CreateTime = d.CreateTime})
                                       .ToList();
            return await Task.Run(async () =>
                ApiResult.Success("查询成功",new {dicList = dicList,total = total}));
        }

        /// <summary>
        /// 修改字典
        /// </summary>
        /// <param name="updateDTO"></param>
        /// <param name="UID"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult> UpdateDictionary(DictionaryUpdateDTO updateDTO, int UID)
        {
             int count =  _repository.UpdateColumns(new servicedictionaries()
                         {
                             sdKey = updateDTO.sdKey,
                             sdValue = updateDTO.sdValue,
                             sdPassed  = updateDTO.sdPassed,
                             ModifyBy = UID,
                             ModifyTime = DateTime.Now
                         },d => d.ID == updateDTO.ID,new string[] { "sdKey", "sdValue", "sdPassed", "ModifyBy", "ModifyTime"});
            return await Task.Run(async () =>
            {
                return count > 0 ? ApiResult.Success("修改成功") : ApiResult.Error("修改失败");
            });
        }
    }
}
